package com.mrd.shiptask_shipowner.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import com.mrd.shiptask_shipowner.R;
import com.mrd.shiptask_shipowner.unit_tools.HttpUrlConstant;
import com.mrd.shiptask_shipowner.unit_tools.SysApplication;
import com.mrd.shiptask_shipowner.unit_tools.VolleyLoadPicture;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ViewPictureActivity extends Activity {

	/** 全局的LayoutInflater对象，已经完成初始化. */
	public LayoutInflater mInflater;
	private final ViewPictureActivity THIS = ViewPictureActivity.this;
	private Button backToBeforeBt13, qianButton, houButton;
	private TextView imageCountV;
	private ImageView imageShow;
	private List<String> mData = new ArrayList<String>();
	private int showPosition = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_picture_activity_manifest);
		initView();
		initDataAction();
		SysApplication.getInstance().addActivity(this);
	}

	private void initDataAction() {
		// TODO Auto-generated method stub
		try {

			String getResult = getIntent().hasExtra("result") == false ? ""
					: getIntent().getStringExtra("result");
			String jumpType = getIntent().hasExtra("jumpType") == false ? ""
					: getIntent().getStringExtra("jumpType");
			JSONArray jas = new JSONArray(getResult);
			for (int i = 0; i < jas.length(); i++) {
				String id = jas.getJSONObject(i).getString("id");
				String type = jas.getJSONObject(i).getString("type");
				if (jumpType.equals(type)) {
					mData.add(id);
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
		} catch (Exception e) {
			// TODO: handle exception
		}
		if (mData.size() > 0) {
			progressDialogShow();
			imageShowPicAction(mData.get(showPosition), showPosition);

		} else if (mData.size() <= 1) {
			qianButton.setVisibility(View.GONE);
			houButton.setVisibility(View.GONE);
		}
	}

	private void initView() {
		TextView topTitleTv = (TextView) findViewById(R.id.topTitleTv);
		topTitleTv.setText("签单浏览");
		backToBeforeBt13 = (Button) findViewById(R.id.backToBeforeBt13);
		backToBeforeBt13.setOnClickListener(socl);
		imageCountV = (TextView) findViewById(R.id.imageCount);
		imageShow = (ImageView) findViewById(R.id.imageView1);
		imageShow.setOnClickListener(socl);
		qianButton = (Button) findViewById(R.id.qianbt);
		houButton = (Button) findViewById(R.id.houbt);
		qianButton.setOnClickListener(socl);
		houButton.setOnClickListener(socl);
	}

	public void imageShowPicAction(String picUrl, int showPosition) {
		showPosition = showPosition + 1;
		imageCountV.setText(showPosition + "/" + mData.size());
		picUrl = HttpUrlConstant.SHIPBIZ_BASE_URL + HttpUrlConstant.GETPICTURE
				+ picUrl;
		// 用Volley加载图片
		VolleyLoadPicture vlp = new VolleyLoadPicture(this, imageShow);
		vlp.getmImageLoader().get(picUrl, vlp.getOne_listener());
	}

	protected final OnClickListener socl = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
			case R.id.backToBeforeBt13:
				finish();
				break;
			case R.id.qianbt:
				try {
					showPosition = showPosition - 1;
					if (showPosition <= 1) {
						showPosition = mData.size() - 1;
					}
					imageShowPicAction(mData.get(showPosition), showPosition);
				} catch (Exception e) {
				}
				break;
			case R.id.houbt:
			case R.id.imageView1:
				try {
					showPosition = showPosition + 1;
					if (showPosition >= mData.size()) {
						showPosition = 0;
					}
					imageShowPicAction(mData.get(showPosition), showPosition);
				} catch (Exception e) {
				}
				break;
			default:
				break;
			}
		}

	};

	/**
	 * 自定义progressDialog，不可手动取消
	 */
	public void progressDialogShow() {
		final ProgressDialog proDia = ProgressDialog.show(THIS, "loading",
				"正在加载，请稍候...", true, false);
		Thread t = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(5000);
					proDia.dismiss();// 关闭ProgressDialog
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		t.start();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
